# 批量生成summary PowerShell脚本

# 获取所有没有summary的demand_id
$query = "SELECT demand_id FROM demand WHERE summary IS NULL OR summary = '';"
$demandIds = mysql -h 47.92.162.251 -P 3306 -u remote_user -pliYC060214pp expert_miniapp_db -sN -e $query 2>&1 | Where-Object { $_ -match "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" }

$total = $demandIds.Count
$count = 0

Write-Host "开始批量生成summary,共 $total 个需求"

foreach ($demandId in $demandIds) {
    $count++
    Write-Host "[$count/$total] 正在生成: $demandId"

    try {
        $response = Invoke-WebRequest -Uri "http://localhost:8080/api/demands/$demandId/regenerate-summary" `
            -Method POST `
            -Headers @{"Content-Type"="application/json"} `
            -UseBasicParsing
        Write-Host "Status: $($response.StatusCode) - 成功"
    } catch {
        Write-Host "Status: 失败 - $($_.Exception.Message)"
    }

    # 避免请求过快,每个请求间隔3秒
    Start-Sleep -Seconds 3
}

Write-Host "批量生成完成!"
